from sqlalchemy import Column, Integer, String, ForeignKey
from model import db
from sqlalchemy.orm import relationship
from model.config import Config
from datetime import datetime

# 定义映射类User，其继承上一步创建的Base
class Question(db.Base):
    # 指定本类映射到表
    __tablename__ = 'question'

    id = Column(Integer, primary_key=True, autoincrement=True)
    type = Column(String)
    content = Column(String)
    order = Column(Integer)

    created_time = Column(String,server_default="CURRENT_TIMESTAMP")
    updated_time = Column(String,server_default="CURRENT_TIMESTAMP")

    options = relationship("QuestionOption",order_by="asc(QuestionOption.order)")
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


class QuestionOption(db.Base):
    # 指定本类映射到表
    __tablename__ = 'question_option'

    id = Column(Integer, primary_key=True, autoincrement=True)
    question_id = Column(Integer,ForeignKey('question.id'))
    ind = Column(String)
    content = Column(String)
    selected = Column(String)
    is_true = Column(String)
    order = Column(Integer)

    created_time = Column(String,server_default="CURRENT_TIMESTAMP")
    updated_time = Column(String,server_default="CURRENT_TIMESTAMP")

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


### 验证是否是合法时间
def vali_date_and_hour(conf:Config)->bool:

    now = datetime.now()

    start_time = str(conf.start_time)
    end_time = str(conf.end_time)

    t_start = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
    t_end = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

    if now < t_start:
        return False
    
    if now > t_end:
        return False 

    return True